package lab5;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.ejb.Stateless;

/**
 *
 * @author grp02 - Pascal Charreix and Meike Paetz
 * This is the flight reservation Web Service
 */
@WebService(serviceName = "grp02FlightReservation")
@Stateless()
public class grp02FlightReservation {

    private static final Map<Integer, FlightReservation> flightReservation = new HashMap<Integer, FlightReservation>();
    private static int count = 0;

    /**
     *
     * This is to initialize Service by annotating post construct
     */
    @PostConstruct
    public void initService() {
        flightReservation.put(count++, new FlightReservation("Paris", "Jönköping", 1318235444, 42, 1, 0, "Air France"));
        flightReservation.put(count++, new FlightReservation("Berlin", "Jönköping", 1318235444, 21, 2, 1, "Lufthansa"));
        flightReservation.put(count++, new FlightReservation("Berlin", "Jönköping", 1318235444, 21, 2, 3, "SAS"));
    }

    /**
     *
     * This is a Web Method to create a flight reservation
     */
    @WebMethod
    public int createFlightReservation(String From, String To, int Date, int SeatNumber, int HollidayPackageId, int Status, String Company) {
        flightReservation.put(count++, new FlightReservation(From, To, Date, SeatNumber, HollidayPackageId, Status, Company));
        return count - 1;
    }

    /**
     *
     * This is a Web Method to get the reservation state
     */
    @WebMethod
    public int getStatus(int FlightReservationID) {
        return (flightReservation.get(FlightReservationID).getStatus());
    }

    /**
     *
     * This is a Web Method to confirm a flight reservation
     */
    @WebMethod
    public int confirmReservation(int FlightReservationID) {
        if (flightReservation.get(FlightReservationID) == null) {
            return -1;
        }
        if (flightReservation.get(FlightReservationID).getStatus() == 0) {
            flightReservation.get(FlightReservationID).setStatus(1);
        }
        return flightReservation.get(FlightReservationID).getStatus();
    }

    /**
     *
     * This is a Web Method to cancel a flight reservation
     */
    @WebMethod
    public int cancelReservation(int FlightReservationID) {
        try {
            flightReservation.remove(FlightReservationID);
        } catch (NullPointerException e) {
            return (-1);
        }
        return 2;
    }

    /**
     *
     * This is a Web Method in order to get info about the reservation
     */
    @WebMethod
    public String getReservationInformation(int FlightReservationID) {
        String result;
        Date myDate;
        FlightReservation Reservation = flightReservation.get(FlightReservationID);
        if (Reservation == null) {
            return "No Reservation Found";
        }
        result = "Information about Reservation number " + FlightReservationID + " :\n";
        result += "From : " + Reservation.getFrom() + "\n";
        result += "To : " + Reservation.getTo() + "\n";
        myDate = new Date(Reservation.getDate());
        result += "Date : " + myDate.toString() + "\n";
        result += "FlyCompany : " + Reservation.getCompany() + "\n";
        result += "Number of seat : " + Reservation.getSeatNumber() + "\n";
        result += "Status : " + Reservation.getStatus() + "\n";
        return result;
    }

    /**
     *
     * This is a Web Method to get info about a confirmed flight reservation
     */
    @WebMethod
    public String getConfirmedInformation() {
        int i = 0;
        FlightReservation Reservation;
        String result = "Information about all Confirmed Reservation :\n";
        while (i < count) {
            Reservation = flightReservation.get(i);
            if (Reservation != null && Reservation.getStatus() == 1) {
                result += getReservationInformation(i);
            }
            i++;
        }
        return result;
    }

    /**
     *
     * This is a Web Method to get info about reservation with state open
     */
    @WebMethod
    public String getOpenInformation() {
        int i = 0;
        FlightReservation Reservation;
        String result = "Information about all Open Reservation :\n";
        while (i < count) {
            Reservation = flightReservation.get(i);
            if (Reservation != null && Reservation.getStatus() == 0) {
                result += getReservationInformation(i);
            }
            i++;
        }
        return result;
    }

    /**
     *
     * This is a Web Method to get listed all flight reservations with state open
     */
    @WebMethod
    public ArrayList<Integer> getOpenList() {
        ArrayList<Integer> arrList = new ArrayList<Integer>();
        int i = 0;
        FlightReservation Reservation;
        while (i < count) {
            Reservation = flightReservation.get(i);
            if (Reservation != null && Reservation.getStatus() == 0) {
                arrList.add(i);
            }
            i++;
        }
        return arrList;
    }

    /**
     *
     * This is a Web Method to get a list with all confirmed flight reservations
     */
    @WebMethod
    public ArrayList<Integer> getConfirmedList() {
        ArrayList<Integer> arrList = new ArrayList<Integer>();
        int i = 0;
        FlightReservation Reservation;
        while (i < count) {
            Reservation = flightReservation.get(i);
            if (Reservation != null && Reservation.getStatus() == 1) {
                arrList.add(i);
            }
            i++;
        }
        return arrList;
    }
}
